package factory.nvwa.extendSingleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author：LJ
 * @Description：
 * @Date: 2017/11/26
 * @Modified By:
 */
public class SingletoFactory {
    private static Singleton singleton;

    static {
        try {
            Class c = Class.forName(Singleton.class.getName());
            //获得无参构造函数
            Constructor constructor = c.getDeclaredConstructor(null);
            //设置无参构造函数可访问
            constructor.setAccessible(true);
            //产生一个实例对象
            singleton = (Singleton) constructor.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public Singleton getSingleton() {
        return singleton;
    }
}
